<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
if($_RQ['op']=='edit'){
	$member=pdo_get('jy_weishop_member',array('mid'=>$_SESSION['uid'],'merchid'=>0,'uniacid'=>$_SESSION['uniacid']));
	$item['yajinfee']=$this->setting['yuejie_price'];
    $return['item']=$item;
	exi($return);
}
if($_RQ['op']=='post'){
	$member=pdo_get('jy_weishop_member',array('mid'=>$_SESSION['uid'],'merchid'=>0,'uniacid'=>$_SESSION['uniacid']));
	if($this->setting['yuejie_price']<=0){
		//exi('无法续期，年费为0！','error');
	}
	$postdata=get_postdata();
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'money'=>$this->setting['yuejie_price'],
			'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_yuejie_viplog',$insert);
	$id=pdo_insertid();
	if($this->setting['yuejie_price']>0){
		$params['fee']=$this->setting['yuejie_price'];
		$params['title']='师傅入驻';
		$params['id']=$id;
		$params['plugin']='jy_yuejie';
		$params['back_url']='jy_yuejie/setyajin/payresult/';
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('viplog',array('pay_id'=>$payid),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$res=array(
			'pay_id'=>$payid,
			'order_id'=>$id,
		);
		exi($res);
	}else{
		pdo_update('jy_yuejie_viplog',array('status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$member['yuejie_endtime']=$member['yuejie_endtime']?:time();
		$update=array('yuejie_type'=>1,'yuejie_endtime'=>$member['yuejie_endtime']+365*24*3600);
		if($this->setting['shoplevel_id']){
			$update['level']=$this->setting['shoplevel_id'];	
		}
		pdo_update('jy_weishop_member',$update,array('id'=>$member['id'],'uniacid'=>$_SESSION['uniacid']));
		$this->model('member')->checkinlog($order['mid']);
		exi('续期成功','success','jy_yuejie/index/');
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result']; 
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_yuejie_viplog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_yuejie_viplog',array('status'=>1),array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
	$member=pdo_get('jy_weishop_member',array('mid'=>$order['mid'],'merchid'=>0,'uniacid'=>$_SESSION['uniacid']));
	$member['yuejie_endtime']=$member['yuejie_endtime']?:time();
	$update=array('yuejie_type'=>1,'yuejie_endtime'=>$member['yuejie_endtime']+365*24*3600);
	if($this->setting['shoplevel_id']){
		$update['level']=$this->setting['shoplevel_id'];	
	}
	pdo_update('jy_weishop_member',$update,array('mid'=>$order['mid'],'merchid'=>0,'uniacid'=>$_SESSION['uniacid']));
	$this->model('member')->checkinlog($order['mid']);
	exi('支付成功','success','jy_yuejie/index/');
}
?>